package cn.xiaou.service.impl;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

import javax.swing.JOptionPane;

import cn.xiaou.dao.UserDao;
import cn.xiaou.dao.impl.UserDaoImpl;
import cn.xiaou.domain.ModifyPwd;
import cn.xiaou.domain.Stock;
import cn.xiaou.domain.Transaction;
import cn.xiaou.domain.User;
import cn.xiaou.domain.UserReal;
import cn.xiaou.service.UserService;
import cn.xiaou.utils.DataSourceUtils;
import cn.xiaou.utils.SendMail;
import cn.xiaou.utils.SmallToolBoxUtils;

public class UserServiceImpl implements UserService {
	private UserDao userdao = new UserDaoImpl();
	@Override
	public Boolean registerUser(User user) {
		Integer state = null;
		try {
			state= userdao.registerUser(user);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
		}
		return state>0;
	}
	@Override
	public boolean isExistUserByUsername(String username) {
		Long state = null;
		try {
			state= userdao.isExistUserByUsername(username);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
		}
		return state==0;
	}
	@Override
	public boolean checkExistEmail(String email) {
		Long state = null;
		try {
			state= userdao.checkExistEmail(email);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
		}
		return state==0;
	}
	@Override
	public User loginUser(String username, String password) {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		try {
			return userdao.loginUser(user);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
			return null;
		}
	}
	@Override
	public void activationMailbox(Integer uid)  {
		try {
			userdao.activationMailbox(uid);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
		}
	}
	@Override
	public Integer modifyPasswordByUsername(String username) {
		
		User user;
		try {
			user = userdao.userByUsername(username);
			if(!isQualifications(user)) {
				return -1;
			}
			return modifyPasswordSendMail(user);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
		}
		return null;
	}
	
	@Override
	public Integer modifyPasswordByEmail(String email) {
		User user;
		try {
			user = userdao.userByEmail(email);
			if(!isQualifications(user)) {
				return -1;
			}
			return modifyPasswordSendMail(user);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
		}
		return null;
	}
	@Override
	public Boolean checkModifyPasswordCodeByCode(String codeStr,Integer modify_id) {
		Long state=0L;
		try {
			state = userdao.checkModifyPwdBycode(codeStr,modify_id);
			/*if(state>0) {
				userdao.updateModifyPwdState(modify_id);
			}*/
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
		}
		return state>0;
	}
	
	@Override
	public Boolean modifyPasswordByModifyId(Integer modify_id,String newPasswordStr) {
		try {
			DataSourceUtils.startTransaction();
			User user = userdao.findUidByModify_id(modify_id);
			user.setPassword(newPasswordStr);
			Integer state = userdao.updatePassword(user);
			if(state!=0) {
				userdao.updateModifyPwdState(modify_id);
			}
			DataSourceUtils.commitAndRelease();
			
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "未知错误33057");
				System.exit(0);
			}
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	private Integer  modifyPasswordSendMail(User user) {
		ModifyPwd modify = new ModifyPwd();
		String code = SmallToolBoxUtils.getRandomInt(6);
		modify.setModify_code(code);
		modify.setUser(user);
		modify.setModify_state("1");
		modify.setModify_date(LocalDateTime.now().format(SmallToolBoxUtils.ofPattern));
		String msg = "你好你的账号现在修改密码\n   验证码是："+code;
		Thread sendMail =  new Thread(new SendMail(user.getEmail(),msg,"修改密码"));
		sendMail.start();
		try {
			return userdao.addModifyPwd(modify);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
			return null;
		}
	}
	private Boolean isQualifications(User user) {
		
		try {
			//1.判断当日修改次数有没有超过5次
			String now =LocalDateTime.now().format(SmallToolBoxUtils.ofPattern2);
			if(userdao.checkModifyCount(user,now)>=5) {
				return false;
			}
			//2.判断当前有没有人正在修改密码
			if(userdao.checkModifyPwding(user)>0) {
				return false;
			}
		}catch(SQLException ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, "未知错误33057");
			System.exit(0);
		}
		
		//3.判断是否是恶意修改
		
		return true;
		
	}
	@Override
	public void updateState(Integer modify_id) {
			try {
				userdao.updateModifyPwdState(modify_id);
			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "未知错误33057");
				System.exit(0);
			}
	}
	@Override
	public Boolean addRealName(UserReal userReal) {
		Integer state = 0;
		try {
			state = userdao.addRealName(userReal);
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return state>0;
	}
	@Override
	public UserReal checkRealnameExist(User user) {
		UserReal userReal  = null;
		try {
			userReal = userdao.checkRealnameExist(user);
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return userReal;
		
	}
	@Override
	public List<Stock> findAllStock() {
		List<Stock> stockList  = null;
		try {
			stockList = userdao.findAllStock();
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return stockList;
	}
	@Override
	public Boolean commitOrder(Transaction tran) {
		try {
			DataSourceUtils.getConnection();
			Integer state = userdao.findDistinct(tran);
			System.out.println(state);
			if(state ==0) {
				userdao.addOrder(tran);
			}else {
				userdao.updateOrder(tran,state);
			}
			userdao.pay(tran);
			userdao.updateStockCount(tran);
			
			DataSourceUtils.commitAndRelease();
			return true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return false;	
	}
	@Override
	public Boolean Recharge(User user, Integer money) {
		Integer state = 0;
		try {
			state = userdao.Recharge(user, money);
		}catch(Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return state >0;
	}
	@Override
	public List<Transaction> findUserTransaction(User user) {
		List<Transaction> tran = null;
		try {
			
			tran = userdao.userTransaction(user);
			for (Transaction transaction : tran) {
				transaction.setStock(userdao.findTransactionBysid(transaction.getSid()));
			}
		}catch(SQLException ex) {
			ex.printStackTrace();
		}
		return tran;
	}
	@Override
	public Boolean sellSock(Transaction order) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Boolean sellStock(Transaction tran, Double money,Integer count) {
		try {
			DataSourceUtils.getConnection();
			userdao.updateCount(tran,count);
			userdao.addMoney(tran.getUser().getUid(),money);
			userdao.updateTransaction(tran);
			if(userdao.checkOrderZero(tran.getTid())==0){
				userdao.deleteTransaction(tran);
			}
			DataSourceUtils.commitAndRelease();
		}catch(Exception ex) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			ex.printStackTrace();
		}
		
		return true;
	}
	
	
}
